<template>
  <view>
    <view class="flex-col justify-center align-center">
      <image src="../../static/images/gogogo.gif" mode="widthFix"></image>
      <view class="margin-tb-sm text-gray">加载中...</view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        session_key: '',
        testId: 27
      };
    },
    computed: {
      userinfo() {
        return this.$store.state.userinfo
      },
      token() {
        return this.$store.state.token
      }
    },
    onLoad() {
      // #ifdef H5
      this.h5test()
      // #endif
      // #ifdef MP
      this.wxLogin()
      // #endif
    },
    methods: {
      h5test() {
        this.$u.get('/login?id=' + this.testId).then(res => {
          let {
            userinfo,
            token
          } = res.data.result

          if (userinfo) {
            this.$store.commit('setUserInfo', userinfo)
            this.$store.commit('setToken', token)
            this.$msg('登录成功', 500, true).then(this.$goto('/pages/home/index', 'tab'))
          }
        })
      },
      wxLogin() {
        uni.login({
          provider: 'weixin',
          success: login => {
            let {
              code
            } = login
            this.getUserInfo(code)
          }
        })
      },
      getUserInfo(code) {
        this.$u.post('/wxlogin', {
          code: code
        }).then(res => {
          let {
            session_key,
            token,
            userinfo
          } = res.data.result

          this.session_key = session_key
          this.$store.commit('setToken', token)

          if (userinfo) {
            this.$store.commit('setUserInfo', userinfo)
            this.$msg('登录成功', 500, true).then(this.$goto(1))
          } else {
            this.$goto('/pages/mine/index', 'tab')
          }
        })
      },
    }
  }
</script>

<style lang="scss">
  page {
    background: #fff;
  }

  .flex-col {
    width: 750rpx;
    height: 100vh;

    image {
      width: 300rpx;
    }
  }
</style>
